<?

/**
  * write_log($message)
  *
  * Author(s): Fabio Dellarosa
  * Date: 26-3-12
  * 
  * Writes the values of certain variables along with a message in a log file.
  *
  * Parameters:
  *  $message:   Message to be logged
  *  $logfile:   Path of log file to write to.
  *
  */
 
 class Logger
 {
    function write_log($message, $logfile='') {
        
        $datedir = date("d-m-Y");
        $datedia= substr($datedir,0,2);
        
            $dir=getcwd();
            
            //echo "[Logger-write_log]<br>DIRE: ".$dir;     //FD DEbug
            //echo "<br>";                //FD DEbug
            
        //list($l1,$l2,$l3)=split('[/\]',$dir);
        
        //define('ABSPATH', '../../../jqSuitePHP_4_1_2_2/');
        if($dir=='C:\xampp\htdocs\SwitchCYS\Logger')
        {   
           // echo "<script>alert('Voy a escribir'); </script>";
            if(!opendir("Logs")) mkdir("Logs",0700);
            
            $logfile="Logs/logs".$datedir.".log";
         }
         else if($dir=='C:\xampp\htdocs\SwitchCYS')
         {
            if(!opendir("Logger/Logs")) mkdir("Logger/Logs",0700);
            
            $logfile="Logger/Logs/logs".$datedir.".log";
         }
         else if($dir=='C:\xampp\htdocs\SwitchCYS\Logger\Logs')
         {             
            $logfile="logs".$datedir.".log";
            
         } 
     
              // Get time of request
              if( ($time = $_SERVER['REQUEST_TIME']) == '') {
                $time = time();
              }
             
              // Get IP address
              if( ($remote_addr = $_SERVER['REMOTE_ADDR']) == '') {
                $remote_addr = "REMOTE_ADDR_UNKNOWN";
              }
             
              // Get requested script
              if( ($request_uri = $_SERVER['REQUEST_URI']) == '') {
                $request_uri = "REQUEST_URI_UNKNOWN";
              }
              // Format the date and time
              $date = date("Y-m-d H:i:s", $time);
             
              // Append to the log file
              if($fd = @fopen($logfile, "a")) {
                $result = fputcsv($fd, array($date, $remote_addr, $request_uri, $message));
               // echo "grabe bien";
                fclose($fd);
             }
    }
    
      //  $result = write_log("An error occurred that prevented a message from being saved");
        
         // write_mysql_log("Something happened!", $db);

}
?>